package com.example.shoppingmallspringboot.controller;

import com.example.shoppingmallspringboot.entity.Goods;
import com.example.shoppingmallspringboot.service.GoodsService;
import com.example.shoppingmallspringboot.util.FwResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController //该注解等价于//@Controller+@ResponseBody
@Tag(name = "GoodsController",description = "商品控制器")
public class GoodsController {
    @Autowired
    GoodsService goodsService;
//    获取全部商品
    @GetMapping("/goods/list")
    @Operation(summary = "获取全部商品",description = "无参")
    public FwResult<List<Goods>> getAllGoods(){
        List<Goods> goods = goodsService.getAllGoods();
        FwResult<List<Goods>> result=FwResult.ok(goods);
        return result;
    }
//    根据类别查找商品
    @GetMapping("/goods/category/{categoryId}")
    @Operation(summary = "根据商品类别获取商品",description = "商品类别id为参数，如果参数为0，则查找全部商品")
    public FwResult<List<Goods>> getGoods(@PathVariable("categoryId") Integer categoryId){
        if (categoryId==0){
            List<Goods> goods = goodsService.getAllGoods();
            FwResult<List<Goods>> result=FwResult.ok(goods);
            return result;
        }
        List<Goods> goods = goodsService.getGoods(categoryId);
        FwResult<List<Goods>> result=FwResult.ok(goods);
        return result;
    }
    //搜索商品
    @GetMapping("/goods/search/{searchText}")
    @Operation(summary = "搜索商品",description = "搜索文本为参数")
    public FwResult<List<Goods>> searchGoods(@PathVariable("searchText") String searchText){
        List<Goods> goods = goodsService.searchGoods(searchText);
        FwResult<List<Goods>> result=FwResult.ok(goods);
        return result;
    }
//根据商品Id获取商品信息
    @GetMapping("/goods/getGoods/{goodsId}")
    @Operation(summary = "根据商品ID获取商品详情",description = "商品ID为参数")
    public FwResult<List<Goods>> getGoodsById(@PathVariable("goodsId") Integer goodsId){
        List<Goods> goods = goodsService.getGoodsById(goodsId);
        FwResult<List<Goods>> result=FwResult.ok(goods);
        return result;
    }
}
